package com.ruoyi.erp.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.erp.mapper.StaffProfileMapper;
import com.ruoyi.erp.domain.StaffProfile;
import com.ruoyi.erp.service.IStaffProfileService;
import com.ruoyi.common.core.text.Convert;

/**
 * 员工档案Service业务层处理
 * 
 * @author sauger
 * @date 2022-03-11
 */
@Service
public class StaffProfileServiceImpl implements IStaffProfileService 
{
    @Autowired
    private StaffProfileMapper staffProfileMapper;

    /**
     * 查询员工档案
     * 
     * @param id 员工档案主键
     * @return 员工档案
     */
    @Override
    public StaffProfile selectStaffProfileById(Integer id)
    {
        return staffProfileMapper.selectStaffProfileById(id);
    }

    /**
     * 查询员工档案列表
     * 
     * @param staffProfile 员工档案
     * @return 员工档案
     */
    @Override
    public List<StaffProfile> selectStaffProfileList(StaffProfile staffProfile)
    {
        return staffProfileMapper.selectStaffProfileList(staffProfile);
    }

    /**
     * 新增员工档案
     * 
     * @param staffProfile 员工档案
     * @return 结果
     */
    @Override
    public int insertStaffProfile(StaffProfile staffProfile)
    {
        //处理已存在情况
        StaffProfile search = new StaffProfile();
        search.setUserId(staffProfile.getUserId());
        List<StaffProfile> exist = this.selectStaffProfileList(search);
        if (!StringUtils.isNull(exist) && exist.size()>0)
        {
            throw new ServiceException("该登录账号已办理员工档案登记");
        }
        search.setUserId(null);
        search.setIdNumber(staffProfile.getIdNumber());
        exist = this.selectStaffProfileList(search);
        if (!StringUtils.isNull(exist) && exist.size()>0)
        {
            throw new ServiceException("该身份证已办理员工档案登记，登录账务为：" + exist.get(0).getLoginName());
        }
        staffProfile.setCreateTime(DateUtils.getNowDate());
        return staffProfileMapper.insertStaffProfile(staffProfile);
    }

    /**
     * 修改员工档案
     * 
     * @param staffProfile 员工档案
     * @return 结果
     */
    @Override
    public int updateStaffProfile(StaffProfile staffProfile)
    {
        staffProfile.setUpdateTime(DateUtils.getNowDate());
        return staffProfileMapper.updateStaffProfile(staffProfile);
    }

    /**
     * 批量删除员工档案
     * 
     * @param ids 需要删除的员工档案主键
     * @return 结果
     */
    @Override
    public int deleteStaffProfileByIds(String ids)
    {
        return staffProfileMapper.deleteStaffProfileByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除员工档案信息
     * 
     * @param id 员工档案主键
     * @return 结果
     */
    @Override
    public int deleteStaffProfileById(Integer id)
    {
        return staffProfileMapper.deleteStaffProfileById(id);
    }

    @Override
    public List<SysUser> selectUnonboardUser(SysUser user) {
        return staffProfileMapper.selectUnonboardUser(user);
    }
}
